'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url admin/dls/kh/cwdz/tongji 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		const db = uniCloud.database();
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			_
		} = util;
		let {
			uid,
			todayStart,
			recharge_source,
			StartTime,
			EndTime,
			querykey
		} = data;
		let res = {
			code: 0,
			msg: "",
			yjyj: 0,
			yjbl: 0
		};
		// 业务逻辑开始-----------------------------------------------------------



		//	检查短剧绑定的秘钥是否为空或是否和用户输入的查询密钥一致，为空或不一致则不允许查询。
		let tvInfo = await db.collection('a_tv').where({
			tv_name: recharge_source || "123123"
		}).get()

		if (tvInfo.data.length == 0) {
			res.code = 1;
			res.msg = "系统中不存在该剧，请核对剧名";
			return res;
		}
		if (userInfo.role.includes("copyright")) {
			if (!tvInfo.data[0].querykey || tvInfo.data[0].querykey != querykey) {
				res.code = 1;
				res.msg = "查询密钥错误";
				return res;
			}
		}


		if (userInfo.role.includes("copyright") || userInfo.role.includes("admin") || userInfo.role.includes(
				"agent" || userInfo.role.includes("middleman"))) {
			console.log("权限校验通过");
		} else {
			res.code = 1;
			res.msg = "用户身份校验不通过";
			return res;
		}




		if (vk.pubfn.isNotNull(StartTime)) {
			if (userInfo.role.includes("admin")) {
				//查询今日充值总金额
				res.jrczje = await vk.baseDao.sum({
					dbName: "a_recharge", // 表名
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						//支付状态必须是已支付
						pay_status: 1,
						tv_name: recharge_source || "123123",
						_add_time: _.and(_.gt(StartTime), _.lt(EndTime)),
					}
				});
				return res;
			}

			if (userInfo.role.includes("middleman")) {
				//查询今日充值总金额
				res.jrczje = await vk.baseDao.sum({
					dbName: "a_recharge", // 表名
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						middleman_id: uid,
						//支付状态必须是已支付
						pay_status: 1,
						tv_name: recharge_source || "123123",
						_add_time: _.and(_.gt(StartTime), _.lt(EndTime)),
					}
				});
				return res;
			}

			if (userInfo.role.includes("agent")) {
				res.jrczje = await vk.baseDao.sum({
					dbName: "a_recharge", // 表名
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						invite_code: uid,
						//支付状态必须是已支付
						pay_status: 1,
						tv_name: recharge_source || "123123",
						_add_time: _.and(_.gt(StartTime), _.lt(EndTime)),
					}
				});
				return res;
			}
			if (userInfo.role.includes("copyright")) {
				//查询今日充值总金额
				res.jrczje = await vk.baseDao.sum({
					dbName: "a_recharge", // 表名
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						//支付状态必须是已支付
						pay_status: 1,
						tv_name: recharge_source || "123123",
						_add_time: _.and(_.gt(StartTime), _.lt(EndTime)),
					}
				});
				return res;
			}
		} else {
			if (userInfo.role.includes("admin")) {
				//查询今日充值总金额
				res.jrczje = await vk.baseDao.sum({
					dbName: "a_recharge", // 表名
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						//支付状态必须是已支付
						pay_status: 1,
						tv_name: recharge_source || "123123",
					}
				});
				return res;
			}

			if (userInfo.role.includes("middleman")) {
				//查询今日充值总金额
				res.jrczje = await vk.baseDao.sum({
					dbName: "a_recharge", // 表名
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						middleman_id: uid,
						//支付状态必须是已支付
						pay_status: 1,
						tv_name: recharge_source || "123123",
					}
				});
				return res;
			}

			if (userInfo.role.includes("agent")) {
				res.jrczje = await vk.baseDao.sum({
					dbName: "a_recharge", // 表名
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						invite_code: uid,
						//支付状态必须是已支付
						pay_status: 1,
						tv_name: recharge_source || "123123",
					}
				});
				return res;
			}

			if (userInfo.role.includes("copyright")) {
				res.jrczje = await vk.baseDao.sum({
					dbName: "a_recharge", // 表名
					fieldName: "shifu_amount", // 需要求和的字段名
					whereJson: {
						//支付状态必须是已支付
						pay_status: 1,
						tv_name: recharge_source || "123123",
					}
				});
				return res;
			}
		}

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}